$(function () {
    var option = {
        url: '../../user/list',
        pagination: true,	//显示分页条
        sidePagination: 'server',//服务器端分页
        showRefresh: true,  //显示刷新按钮
        search: true,
        toolbar: '#toolbar',
        striped: true,     //设置为true会有隔行变色效果
        columns: [
            {checkbox: true},
            {
                field: 'userId',
                title: '序号',
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize = $('#table').bootstrapTable('getOptions').pageSize;
                    var pageNumber = $('#table').bootstrapTable('getOptions').pageNumber;
                    return pageSize * (pageNumber - 1) + index + 1;
                }
            },

            {title: '用户ID', field: 'userId'},
            {field: 'userName', title: '用户名称'},
            {title: '用户电话', field: 'phone'},
            {title: '用户身份', field: 'identity'}
        ]
    };
    $('#table').bootstrapTable(option);
});
var vm1 = new Vue({
    el: '#sysUserDlg',
    data: {
        user: {}
    },
    methods: {
        add: function () {
            $.ajax({
                type: "POST",
                url: "../../user/add",
                data: JSON.stringify(vm1.user),
                success: function (r) {
                    if(r>0){
                        alert("添加成功");
                    }else {
                        alert("添加失败");
                    }
                    window.location.href="http://localhost:8081/html/sysUser/sysUserForm.html";
                }
            });
        }
    }
});
var vm = new Vue({
    el: '#dtapp',
    data: {
        showList: true,
        title: null,
        user: {}
    },
    methods: {
        del: function () {
            var rows = getSelectedRows();
            if (rows == null) {
                return;
            }
            var id = 'userId';
            //提示确认框
            layer.confirm('您确定要删除所选数据吗？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function (index, layero) {
                var ids = new Array();
                //遍历所有选择的行数据，取每条数据对应的ID
                $.each(rows, function (i, row) {
                    ids[i] = row[id];
                });

                $.ajax({
                    type: "POST",
                    url: "../../user/del",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r>0) {
                            layer.alert('删除成功');
                            $('#table').bootstrapTable('refresh');
                        } else {
                            layer.alert("删除失败");
                        }
                    },
                    error: function () {
                        layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                    }
                });
            });
        },
        selectInfo: function () {
            var id = 'userId';
            var userId = getSelectedRow()[id];
            if (userId == null) {
                return;
            }

            $.get("../../user/info/" + userId, function (r) {
                vm.showList = false;
                vm.title = "修改";
                vm.user = r;
            });
        },
        reload: function (event) {
            vm.showList = true;
            $("#table").bootstrapTable('refresh');
        },
        update:function () {
            $.ajax({
                type: "POST",
                url: "../../user/update",
                data: JSON.stringify(vm.user),
                success: function(r){
                    if(r>0){
                        layer.alert('操作成功', function(index){
                            layer.close(index);
                            vm.reload();
                        });
                    }else{
                        layer.alert("操作失败");
                    }
                }
            });
        }
    }
});